package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityExternalGoodsCategoryDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoodsCategory;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoodsCategory.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoodsCategoryExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoodsCategoryExample.Criteria;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityExternalGoodsCategoryManager {
	
    @Autowired
    private EstateProjectActivityExternalGoodsCategoryDAO dao;
    
    public EstateProjectActivityExternalGoodsCategory getCategory(Integer externalGoodsCategoryId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(externalGoodsCategoryId, EstateProjectActivityExternalGoodsCategory.NOT_DELETED);
	}
    
    public List<EstateProjectActivityExternalGoodsCategory> childrenCategory(Integer externalGoodsCategoryId) {
    	EstateProjectActivityExternalGoodsCategoryExample example
		= new EstateProjectActivityExternalGoodsCategoryExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andParentCategoryIdEqualTo(externalGoodsCategoryId)
    	.andDeletedEqualTo(EstateProjectActivityExternalGoodsCategory.NOT_DELETED);
    	return dao.selectByExample(example);
	}

	public Pagination<EstateProjectActivityExternalGoodsCategory> queryCategory(Integer projectId, String name, Sorting[] sorts, Page page) {
		EstateProjectActivityExternalGoodsCategoryExample example
			= new EstateProjectActivityExternalGoodsCategoryExample();
		Criteria criteria = example.createCriteria();
		if(Check.notNullOrEmpty(name)) {
			criteria.andNameLike("%"+name+"%");
		}
		criteria.andProjectIdEqualTo(projectId).andDeletedEqualTo(EstateProjectActivityExternalGoodsCategory.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	public Pagination<EstateProjectActivityExternalGoodsCategory> queryCategoryForH5(Integer projectId, Page page) {
		EstateProjectActivityExternalGoodsCategoryExample example
		= new EstateProjectActivityExternalGoodsCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andProjectIdEqualTo(projectId)
		.andOnlineEqualTo(true)
		.andDeletedEqualTo(EstateProjectActivityExternalGoodsCategory.NOT_DELETED);
		example.setOffset(page.getFirstIndex());
		example.setRows(page.getPageSize());
		example.orderBy(Column.weight.desc());
		return Pagination.create(page, 
			dao.countByExample(example),
			() -> dao.selectByExample(example));
	}

	public EstateProjectActivityExternalGoodsCategory addCategory(EstateProjectActivityExternalGoodsCategory record, String createUser){
		record.setOnline(false);
		record.setDeleted(EstateProjectActivityExternalGoodsCategory.NOT_DELETED);
		record.setCreateUser(createUser);
		record.setCreateTime(Date.from(Instant.now()));
		record.setUpdateUser(createUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.insert(record);
		return record;
	}

	public EstateProjectActivityExternalGoodsCategory editCategory(EstateProjectActivityExternalGoodsCategory record, String updateUser){
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.excludes(Column.online,Column.deleted,Column.createUser,Column.createTime));
		return record;
	}

	public void deleteCategory(EstateProjectActivityExternalGoodsCategory record, String updateUser){
		record.setDeleted(EstateProjectActivityExternalGoodsCategory.IS_DELETED);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.deleted,Column.updateUser,Column.updateTime);
	}

	public void onlineCategory(EstateProjectActivityExternalGoodsCategory record, String updateUser){
		record.setOnline(true);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}

	public void offlineCategory(EstateProjectActivityExternalGoodsCategory record, String updateUser){
		record.setOnline(false);
		record.setUpdateUser(updateUser);
		record.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(record, Column.online,Column.updateUser,Column.updateTime);
	}
}